package com.dartfar.workflow.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.dartfar.base.action.BaseAction;
import com.dartfar.base.vo.DataGridVo;
import com.dartfar.util.EntityUtils;
import com.dartfar.workflow.constant.WorkflowCaleType;
import com.dartfar.workflow.constant.WorkflowEventType;
import com.dartfar.workflow.model.WorkflowEvents;
import com.dartfar.workflow.service.WorkflowEventsService;
import com.dartfar.workflow.service.WorkflowProcessFunMountService;
import com.dartfar.workflow.service.WorkflowTaskFunMountService;
import com.dartfar.workflow.vo.WorkflowEntrustVo;
import com.dartfar.workflow.vo.WorkflowEventTypeVo;
import com.dartfar.workflow.vo.WorkflowEventsVo;

/**
 * 流程任务事件Action
 * @author 王琦
 *
 */
@Scope("prototype")
@Component("workflowEventsAction")
public class WorkflowEventsAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	static Logger logger = Logger.getLogger(WorkflowEventsAction.class);
	
	/**
	 * Spring 注入
	 */
	@Resource
	private WorkflowEventsService workflowEventsService;
	
	@Resource
	private WorkflowTaskFunMountService workflowTaskFunMountService;
	
	@Resource
	private WorkflowProcessFunMountService workflowProcessFunMountService;
	
	/**
	 * 页面传递
	 */
	private int id;
	
	private String inserted;
	private String updated;
	private String deleted;
	

	/**
	 * 保存任务事件 
	 * @return
	 */
	public String saveWorkflowEvents(){
		List<WorkflowEventsVo> list;
		
		if(inserted != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(inserted, WorkflowEventsVo.class);
			for(Iterator<WorkflowEventsVo> w =  list.iterator();w.hasNext();){
				WorkflowEventsVo weVo = w.next();
				addWorkflowEvents(weVo);
			}
		}
		//更新键值
		if(updated != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(updated, WorkflowEventsVo.class);
			for(Iterator<WorkflowEventsVo> i =  list.iterator();i.hasNext();){
				WorkflowEventsVo weVo = i.next();
				editWorkflowEvents(weVo);
			}
		}
		//删除键值
		if(deleted != null){
			//把JSON字符串转换成对象
			list = JSON.parseArray(deleted, WorkflowEventsVo.class);
			for(Iterator<WorkflowEventsVo> i =  list.iterator();i.hasNext();){
				WorkflowEventsVo weVo = i.next();
				removeWorkflowEvents(weVo);
			}
		}
		
		return null;
	}
	
	/**
	 * 增加任务事件
	 * @param weVo
	 * @return
	 */
	public void addWorkflowEvents(WorkflowEventsVo weVo){
		try{
			WorkflowEvents we = new WorkflowEvents();
			we.setProcessDefinitionId(weVo.getProcessDefinitionId());
			we.setTaskName(weVo.getTaskName());
			we.setEventType(weVo.getEventType());
			we.setEventScope(weVo.getEventScope());
			we.setExecuteStrategy(weVo.getExecuteStrategy());
			we.setOrderIndex(weVo.getOrderIndex());
			we.setServiceId(weVo.getServiceId());
			we.setServiceMethod(weVo.getServiceMethod());
			we.setMethodParams(weVo.getMethodParams());
			logger.info("WorkFlowProcessID: " + workflowProcessFunMountService.get(weVo.getWorkflowProcessFunMountId()));
			logger.info("WorkFlowTaskID: " + workflowTaskFunMountService.get(weVo.getWorkflowTaskFunMountId()));
			we.setWorkflowProcessFunMount(workflowProcessFunMountService.get(weVo.getWorkflowProcessFunMountId()));
			we.setWorkflowTaskFunMount(workflowTaskFunMountService.get(weVo.getWorkflowTaskFunMountId()));
			workflowEventsService.save(we);
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

	}
	
	/**
	 * 编辑任务事件
	 * @param weVo
	 */
	public void editWorkflowEvents(WorkflowEventsVo weVo){ 
		WorkflowEvents we = workflowEventsService.get(weVo.getId());
		//we.setProcessDefinitionId(weVo.getProcessDefinitionId());
		//we.setTaskName(weVo.getTaskName());
		we.setEventType(weVo.getEventType());
		we.setEventScope(weVo.getEventScope());
		we.setExecuteStrategy(weVo.getExecuteStrategy());
		we.setOrderIndex(weVo.getOrderIndex());
		we.setServiceId(weVo.getServiceId());
		we.setServiceMethod(weVo.getServiceMethod());
		we.setMethodParams(weVo.getMethodParams());
		//we.setWorkflowTaskFunMount(workflowTaskFunMountService.get(weVo.getWorkflowTaskFunMountId()));
		workflowEventsService.update(we);
	}
	
	/**
	 * 删除任务事件
	 * @param weVo
	 */
	public void removeWorkflowEvents(WorkflowEventsVo weVo){ 
		WorkflowEvents we = workflowEventsService.get(weVo.getId());
		workflowEventsService.delete(we);
	}
	
	/**
	 * 加载工作流流程事件数据
	 * @return JSON
	 */
	public String loadWorkflowTaskEventsDataByProcessFunMount(){	
		DataGridVo<WorkflowEventsVo> dataGridVos = new DataGridVo<WorkflowEventsVo>();
		List<WorkflowEventsVo> workflowEventsVo = new ArrayList<WorkflowEventsVo>();
		//列表
		List<WorkflowEvents> list = workflowEventsService.getListForPageByWorkflowProcessId(id, pageNo, pageSize);
		//记录数
		Long count = workflowEventsService.getCountByWorkflowProcessId(id);
		
		for(Iterator<WorkflowEvents> w =  list.iterator(); w.hasNext();){
			WorkflowEvents we = w.next();
			WorkflowEventsVo weVo = new WorkflowEventsVo();
			try {
				//拷贝
				BeanUtils.copyProperties(weVo, we);
				if(EntityUtils.isNotNull(weVo.getEventType())){
					weVo.setEventType(WorkflowEventType.getWorkflowEventTypeName(weVo.getEventType()));
				}
				workflowEventsVo.add(weVo);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		//设置VO
		dataGridVos.setTotal(count);
		dataGridVos.setRows(workflowEventsVo);
		String jsonResult = JSONObject.fromObject(dataGridVos).toString();
		logger.info(jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}	
		return null;
	}
	
	/**
	 * 加载工作流任务事件数据
	 * @return JSON
	 */
	public String loadWorkflowTaskEventsDataByTaskFunMount(){
		DataGridVo<WorkflowEventsVo> dataGridVos = new DataGridVo<WorkflowEventsVo>();
		List<WorkflowEventsVo> workflowEventsVo = new ArrayList<WorkflowEventsVo>();
		//列表
		List<WorkflowEvents> list = workflowEventsService.getListForPageByWorkflowTaskId(id, pageNo, pageSize);
		//记录数
		Long count = workflowEventsService.getCountByWorkflowProcessId(id);
		
		for(Iterator<WorkflowEvents> w =  list.iterator(); w.hasNext();){
			WorkflowEvents we = w.next();
			WorkflowEventsVo weVo = new WorkflowEventsVo();
			try {
				//拷贝
				BeanUtils.copyProperties(weVo, we);
				if(EntityUtils.isNotNull(weVo.getEventType())){
					weVo.setEventType(WorkflowEventType.getWorkflowEventTypeName(weVo.getEventType()));
				}
				workflowEventsVo.add(weVo);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		//设置VO
		dataGridVos.setTotal(count);
		dataGridVos.setRows(workflowEventsVo);
		String jsonResult = JSONObject.fromObject(dataGridVos).toString();
		logger.info(jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 加载流程事件类型数据
	 * 
	 */
	public void loadWorkflowProcessEventTypeData(){
		List<WorkflowEventTypeVo> workflowEventTypeVos = new ArrayList<WorkflowEventTypeVo>();
		WorkflowEventTypeVo wetVo1 = new WorkflowEventTypeVo();
		wetVo1.setId("PROCESS_START");
		wetVo1.setName("流程启动");
		workflowEventTypeVos.add(wetVo1);
		
		WorkflowEventTypeVo wetVo2 = new WorkflowEventTypeVo();
		wetVo2.setId("PROCESS_END");
		wetVo2.setName("流程结束");
		workflowEventTypeVos.add(wetVo2);
		
		String jsonResult = JSONArray.fromObject(workflowEventTypeVos).toString();
		logger.info(jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 加载任务事件类型数据
	 * 
	 */
	public void loadWorkflowTaskEvetTypeData(){
		List<WorkflowEventTypeVo> workflowEventTypeVos = new ArrayList<WorkflowEventTypeVo>();
		WorkflowEventTypeVo wetVo3 = new WorkflowEventTypeVo();
		wetVo3.setId("SEND_BEAFORE");
		wetVo3.setName("发送之前");
		workflowEventTypeVos.add(wetVo3);
		
		WorkflowEventTypeVo wetVo4 = new WorkflowEventTypeVo();
		wetVo4.setId("SEND_AFTER");
		wetVo4.setName("发送之后");
		workflowEventTypeVos.add(wetVo4);
		
		WorkflowEventTypeVo wetVo5 = new WorkflowEventTypeVo();
		wetVo5.setId("ROLLBACK_BEAFORE");
		wetVo5.setName("回退之前");
		workflowEventTypeVos.add(wetVo5);
		
		WorkflowEventTypeVo wetVo6 = new WorkflowEventTypeVo();
		wetVo6.setId("ROLLBACK_AFTER");
		wetVo6.setName("回退之后");
		workflowEventTypeVos.add(wetVo6);
		
		WorkflowEventTypeVo wetVo7 = new WorkflowEventTypeVo();
		wetVo7.setId("RETURN_BEFORE");
		wetVo7.setName("取回之前");
		workflowEventTypeVos.add(wetVo7);
		
		WorkflowEventTypeVo wetVo8 = new WorkflowEventTypeVo();
		wetVo8.setId("RETURN_AFTER");
		wetVo8.setName("取回之后");
		workflowEventTypeVos.add(wetVo8);
		
		String jsonResult = JSONArray.fromObject(workflowEventTypeVos).toString();
		logger.info(jsonResult);
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
	}


	/**
	 * Getter And Setter
	 * 
	 */
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getInserted() {
		return inserted;
	}

	public void setInserted(String inserted) {
		this.inserted = inserted;
	}

	public String getUpdated() {
		return updated;
	}

	public void setUpdated(String updated) {
		this.updated = updated;
	}

	public String getDeleted() {
		return deleted;
	}

	public void setDeleted(String deleted) {
		this.deleted = deleted;
	}

	
	
}
